<template>
  <div id="search">
    <van-search
      @input="OnInput"
      v-model.trim="value"
      shape="round"
      placeholder="请输入搜索关键词"
    />
    <div v-if="!searchList.length" class="hotSearch">
      <p>热门搜索</p>
      <div class="hotTags">
        <span
          @click="onClickTags(item.first)"
          v-for="(item, index) in hotSearch"
          :key="index"
          :class="{ hotred: index <= 2 }"
          >{{ item.first }}</span
        >
      </div>
    </div>
    <div v-else class="searchRes">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div class="search_header">
          <p>单曲</p>
          <PlayAll :songData="songData" />
        </div>
        <MusicItem
          v-for="(item, index) in searchList"
          :key="index"
          :name="item.name"
          :picUrl="item.al.picUrl"
          :artists="item.ar[0].name"
          :id="item.id"
          :duration="item.dt"
          :mvId="item.mv"
        />
      </van-list>
    </div>
  </div>
</template>

<script>
import PlayAll from '@/components/PlayAll'
import MusicItem from '@/components/MusicItem'
import { getHotSearch, getSearchRes } from '@/api/search.js'
export default {
  name: 'Search',
  data () {
    return {
      value: '',
      hotSearch: [],
      searchList: [],
      loading: false,
      finished: false,
      page: 1,
      songData: []
    }
  },
  components: {
    MusicItem,
    PlayAll
  },
  created () {
    this.getHotSearchLoad()
  },
  methods: {
    // 关键字
    async getHotSearchLoad () {
      const res = await getHotSearch()
      this.hotSearch = res.result.hots
    },

    // 点击关键字搜索
    onClickTags (value) {
      this.value = value
      this.getSearchResLoad({ offset: 0 })
    },
    // 输入搜索
    OnInput () {
      this.page = 1
      if (this.value === '') {
        this.searchList = []
        clearTimeout(this.timeId)
        return
      }
      if (this.timeId) {
        clearTimeout(this.timeId)
      }
      this.timeId = setTimeout(() => {
        this.getSearchResLoad({ offset: 0 })
      }, 800)
    },
    async onLoad () {
      // 页码
      this.page++
      // 1. 发送请求
      // 2. 保存数据
      await this.getSearchResLoad({ offset: (this.page - 1) * 10 })
      // 3. 关闭loading
      this.loading = false
      // 所有数据加载了 不去加载下一个 finished 为 true
      // 数据没有加载完成 加载下一页
    },
    // 搜索
    async getSearchResLoad (offset) {
      const res = await getSearchRes({
        keywords: this.value,
        limit: 10,
        ...offset
      })
      if (res.result.songCount) {
        this.searchList.push(...res.result.songs)
        res.result.songs.forEach(item => {
          this.songData.push({
            name: item.name,
            picUrl: item.al.picUrl,
            mid: item.id,
            artName: item.ar[0].name,
            duration: item.dt
          })
        })
      } else {
        this.finished = true
      }
    }
  }
}
</script>

<style scoped lang="less">
.hotSearch {
  padding: 0 10px;
  p {
    font-size: 14px;
    color: #999;
    text-align: left;
    margin: 10px 0;
  }
  .hotTags {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    span {
      font-size: 13px;
      color: #666;
      border: 1px solid #ccc;
      border-radius: 15px;
      padding: 7px 10px;
      margin: 7px 0;
      margin-right: 8px;
    }
  }
  .hotred {
    border: 1px solid rgb(255, 80, 80) !important;
    color: rgb(252, 85, 85) !important;
  }
}
.searchRes {
  .search_header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 20px;
    p {
      font-size: 14px;
    }
  }
}
</style>
